from google.appengine.ext import db
from google.appengine.ext import blobstore
from google.appengine.ext import search

GENDER_CHOICES = (
      'Male',
      'Female',
    )

YESNO_CHOICES = (
      0,
      1,
    )

ASSOC_CHOICES = (
	'facebook',
	'youtube',
	'twitter',
	)


class Gabusinesstype(db.Model):
    gabusinesstype= db.StringProperty(required=True)
    date_created = db.DateTimeProperty(auto_now_add=True)
    date_updated = db.DateTimeProperty(auto_now=True)
    created_by = db.IntegerProperty()  # numbers for phone models
    modified_by= db.IntegerProperty()

    def __unicode__(self):
        return self.gabusinesstype

    

class Gabusiness(db.Model):
    biztype = db.ReferenceProperty(Gabusinesstype, collection_name='businessoftype' ) #N for non profit, T for taxable
    bizname = db.StringProperty(required=True)
    contactname = db.StringProperty(required=True)
    contactemail = db.EmailProperty(required=True)
    supportemail = db.EmailProperty()
    phonenumber = db.PhoneNumberProperty(required=True)
    faxnumber = db.PhoneNumberProperty()
    password = db.StringProperty() #change for ver 3 and 4
    image = db.BlobProperty()
    image_meta = db.BlobProperty()
    ein = db.StringProperty(default='0000')
    external_biz_id = db.StringProperty()
    url = db.LinkProperty()
    is_active = db.BooleanProperty(default=True) # invalid for adding events if they are not active (not paid, etc)
    created_by = db.IntegerProperty()  # numbers for phone models 1 iphone 2 android 0 admin user 3 website
    modified_by= db.IntegerProperty()
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True )
    last_login = db.DateTimeProperty() #added for 4

    def __unicode__(self):
      return self.bizname


class Gabizlocation(db.Model):
    gabusiness = db.ReferenceProperty(Gabusiness, collection_name='business_locations')
    geopoint = db.GeoPtProperty()
    address = db.StringProperty() #changed this whole thing
    address2 = db.StringProperty() # changed for 4
    address_full = db.PostalAddressProperty() #changed for 4
    city = db.StringProperty()
    state = db.StringProperty()
    zipcode = db.StringProperty()
    country = db.StringProperty()
    created_by = db.IntegerProperty()  # numbers for phone models
    modified_by= db.IntegerProperty()
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True )


    def __unicode__(self):
        address = self.address + ', '+  self.city + ', ' + self.state
        return address
"""
		event stuff
"""

class Gacategory(db.Model):
    categoryname = db.StringProperty()
    categoryparentname = db.SelfReferenceProperty(collection_name='subcategories')
    created_by = db.IntegerProperty()  # 0 for admin, 
    modified_by= db.IntegerProperty()
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True )
    is_active = db.IntegerProperty() # 0,1, -1/2 for different actives

    def __unicode__(self):
	return self.categoryname

class Gaevent(search.SearchableModel):
    eventname = db.StringProperty(required=True)
    eventdescription = db.TextProperty(required=True)
    #image = db.BlobProperty()
    #image_meta = db.BlobProperty()
    tickets = db.LinkProperty()
    gabusiness = db.ReferenceProperty(Gabusiness, collection_name='business_events',required=True)
    gacategory = db.ReferenceProperty(Gacategory, collection_name='event_categorys')
    #gacategory1 = db.ReferenceProperty(Gacategory, collection_name='event_categorys1')
    #gacategory2 = db.ReferenceProperty(Gacategory, collection_name='event_categorys2')
    eventcategories = db.ListProperty(int)
    eventgeopoint = db.GeoPtProperty()
    supportemail = db.EmailProperty()
    supportphone = db.PhoneNumberProperty()
    eventstartdate = db.DateProperty()
    eventenddate = db.DateProperty()
    eventstarttime = db.TimeProperty()
    eventendtime = db.TimeProperty()
    eventexpiration = db.DateTimeProperty()
    eventstartdatetime = db.DateTimeProperty()
    eventenddatetime = db.DateTimeProperty()
    is_active = db.IntegerProperty() # 0 is just a draft, 1 is published, 2 is expired, -1 is deleted
    is_featured = db.BooleanProperty()
    price = db.StringProperty()
    likecount = db.IntegerProperty(default = 0)
    created_by = db.IntegerProperty()  # numbers for admin, website
    modified_by= db.IntegerProperty()
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True )

    unsearchable_properties = [
                        'eventcategories', 'price', 'supportemail', 
    ]
    def __unicode__(self):
        event = self.eventname + " (" + str(self.eventstartdate) + ")"
        return event
    @classmethod
    def Searchable_Properties(cls):
        return [['eventname'], ['eventdescription'], ['eventname','eventdescription']]

class Gaeventlocation(db.Model):
    gaevent = db.ReferenceProperty(Gaevent, collection_name= 'event_locations')
    geopoint = db.GeoPtProperty()
    address = db.StringProperty() #changed this whole thing
    address2 = db.StringProperty() # changed for 4
    address_full = db.PostalAddressProperty() #changed for 4
    eventstartdatetime = db.DateTimeProperty()
    eventenddatetime = db.DateTimeProperty()
    city = db.StringProperty()
    state = db.StringProperty()
    zipcode = db.StringProperty()
    country = db.StringProperty()
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True )

    def __unicode__(self):
        address = self.address + ', '+  self.city + ', ' + self.state
        return address


    
class Gaeventcategory(db.Model):
    gaevent = db.ReferenceProperty(Gaevent, collection_name = 'event_categories')
    gacategory = db.ReferenceProperty(Gacategory, collection_name='categorys_events')
    eventstartdatetime = db.DateTimeProperty()
    eventenddatetime = db.DateTimeProperty()
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True )

class Gaeventimages(db.Model):
    gaevent = db.ReferenceProperty(Gaevent, collection_name='event_images')
    image1 = db.BlobProperty()
    image1_meta = db.BlobProperty()
    image2 = db.BlobProperty()
    image2_meta = db.BlobProperty()
    image3 = db.BlobProperty()
    image3_meta = db.BlobProperty()
    image4 = db.BlobProperty()
    image4_meta = db.BlobProperty()
    mainimage = db.IntegerProperty(choices= (1,2,3,4))
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True )
    #might need to add a main image.. for the thumbnail and main picture

class Gaeventvideo(db.Model):
    gaevent = db.ReferenceProperty(Gaevent, collection_name='event_videos')
    video = db.LinkProperty()
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True )


"""
	user models (django style)
"""

#07/11/11 added email/username lower for easy checking of duplicates

class Gauser(db.Model):
    email = db.EmailProperty(required=True)
    firstname = db.StringProperty(verbose_name = 'First Name', required=True)
    lastname = db.StringProperty( verbose_name = 'Last Name',required=True)
    username = db.StringProperty(required=True)
    currentzip = db.StringProperty()
    gender = db.StringProperty(required=True,choices=GENDER_CHOICES)
    birthday = db.DateProperty(required=True)
    password = db.StringProperty(required=True)
    image = db.BlobProperty()
    image_meta = db.BlobProperty()
    #image = blobstore.BlobReferenceProperty()
    is_active = db.BooleanProperty(default=True)
    is_public = db.BooleanProperty(default=True)
    is_calendar_shared = db.BooleanProperty(default=True)
    deviceid = db.StringProperty()
    created_by = db.IntegerProperty() # numbers for phone models
    modified_by= db.IntegerProperty()
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True )
    last_login = db.DateTimeProperty()
    #test = db.ListProperty(str)

    def __unicode__(self):
      name = self.firstname + " " + self.lastname
      email = str(self.email)
      return name + " (" + email + ")"


class Gauserinbox(db.Model):
    gauserfrom = db.ReferenceProperty(Gauser, collection_name='user_sentmessages',required=True)
    gauserto = db.ReferenceProperty(Gauser, collection_name='user_receivedmessages', required=True)
    subject = db.StringProperty(required=True)
    message = db.TextProperty(required=True)
    thread_id = db.StringProperty() #maybe integer property
    is_read = db.BooleanProperty(default=False)
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True )

class Gausernotificationtypes(db.Model):
    notificationtype = db.StringProperty(required=True)
    date_created = db.DateTimeProperty(auto_now_add=True)
    date_updated = db.DateTimeProperty(auto_now=True)
    created_by = db.IntegerProperty()  # numbers for phone models
    modified_by= db.IntegerProperty()

    def __unicode__(self):
        return self.notificationtype

class Gausernotification(db.Model):
    notificationby = db.ReferenceProperty(Gauser, collection_name='user_sentnotifications')
    notificationfor = db.ReferenceProperty(Gauser, collection_name='user_receivednotifications')
    event = db.ReferenceProperty(Gaevent, collection_name = 'event_notifications')
    notificationtype = db.ReferenceProperty(Gausernotificationtypes, collection_name='notificationsoftype') # i.e. upcoming events, featured this week
    text = db.TextProperty() 
    is_read = db.BooleanProperty(default=False)
    created_by = db.IntegerProperty()  # numbers for phone models
    modified_by= db.IntegerProperty()
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True)

class Gauserfriend(db.Model):
    gauser = db.ReferenceProperty(Gauser, collection_name='user_friends', required=True)
    friend = db.ReferenceProperty(Gauser, collection_name='friended_users', required=True)
    status = db.IntegerProperty(default=0)  #-1 for deleted, 0 for unconfirmed, 1 for accepted
    created_by = db.IntegerProperty()  # numbers for phone models
    modified_by= db.IntegerProperty()
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True )

#need to add other stuff for easy sorting of user's events
#07/11/11 added eventstartdatetime/enddatetime for sorting, eventstartdate/eventenddate/eventstarttime/eventendtime/event geocode/event category(s) for filtering
class Gauseraddedevent(db.Model):
    gauser = db.ReferenceProperty(Gauser, collection_name='user_events')
    gaevent = db.ReferenceProperty(Gaevent, collection_name='events_users')
    buyticket = db.BooleanProperty()
    is_public = db.BooleanProperty()
    is_attending = db.IntegerProperty() #yes no maybe
    is_favorite = db.BooleanProperty() #
    eventstartdatetime = db.DateTimeProperty()
    eventenddatetime = db.DateTimeProperty()
    eventstartdate = db.DateProperty()
    eventenddate = db.DateProperty()
    eventstarttime = db.TimeProperty()
    eventendtime = db.TimeProperty()
    eventgeopoint = db.GeoPtProperty()
    eventcategory = db.ListProperty(int)
    created_by = db.IntegerProperty()  # numbers for phone models
    modified_by= db.IntegerProperty()
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True )


class Gauseraddedbusiness(db.Model):
    gauser = db.ReferenceProperty(Gauser, collection_name='user_businesses')
    gabusiness  = db.ReferenceProperty(Gabusiness, collection_name='business_users')
    created_by = db.IntegerProperty()  # numbers for phone models
    modified_by= db.IntegerProperty()
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True )

class Gauserlocation(db.Model):
    gauser = db.ReferenceProperty(Gauser, collection_name= 'user_locations')
    is_primary = db.BooleanProperty(default=False)
    geopoint = db.GeoPtProperty()
    address = db.StringProperty()
    address2 = db.StringProperty()
    address_full = db.PostalAddressProperty()
    city = db.StringProperty()
    state = db.StringProperty()
    zipcode = db.StringProperty()
    country = db.StringProperty()
    created_by = db.IntegerProperty()  # numbers for phone models
    modified_by= db.IntegerProperty()
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True )

class Gaeventrecommendation(db.Model):
    gaevent = db.ReferenceProperty(Gaevent, collection_name='recommended_event')
    recommendationfor = db.ReferenceProperty(Gauser,collection_name='user_receivedrecommendations' )
    recommendationby = db.ReferenceProperty(Gauser, collection_name='user_sentrecommendations')
    created_by = db.IntegerProperty()  # numbers for phone models
    modified_by= db.IntegerProperty()
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True )	


class Gauserassociation(db.Model):
    gauser = db.ReferenceProperty(Gauser, collection_name= 'user_associations')
    associationid = db.StringProperty(required=True)
    associationtype = db.StringProperty(required=True,choices=ASSOC_CHOICES)
    date_updated = db.DateTimeProperty(auto_now=True)
    date_created = db.DateTimeProperty(auto_now_add=True )
